﻿/*
 * Jonathan
 * Created by: Jonathan Montiverdi
 * Date: 7/19/2012
 * License: http://jonathan.codeplex.com/license
 */
using System.Web;

namespace Jonathan.Data
{
   /// <summary>
   /// Description of Link.
   /// </summary>
   public class Link
   {
      private string m_Link;      
      /// <summary>
      /// Initializies a new link.
      /// </summary>
      /// <param name="link">Link we are initializing.</param>
      public Link (string link = "")
      {
         this.Parse (link);
      }
      
      /// <summary>
      /// Gets the original link.
      /// </summary>
      public string FullLink { get { return this.m_Link; } }
      
      /// <summary>
      /// Gets or Sets the book.
      /// </summary>
      public string Book { get; private set; }
      
      /// <summary>
      /// Gets or Sets the sub book.
      /// </summary>
      public string SubBook { get; private set; }
      
      /// <summary>
      /// Gets or Sets the chapter.
      /// </summary>
      public string Chapter { get; private set; }
      
      /// <summary>
      /// Gets or Sets the verse.
      /// </summary>
      public string Verse { get; private set; }
      
      /// <summary>
      /// Gets if this link has a book set.
      /// </summary>
      public bool HasBook { get { return !string.IsNullOrEmpty (this.Book); } }
      
      /// <summary>
      /// Gets if this link has a sub book set.
      /// </summary>
      public bool HasSubBook { get { return !string.IsNullOrEmpty (this.SubBook); } }
      
      /// <summary>
      /// Gets if this link has a chapter set.
      /// </summary>
      public bool HasChapter { get { return !string.IsNullOrEmpty (this.Chapter); } }
      
      /// <summary>
      /// Gets if this link has a verse set.
      /// </summary>
      public bool HasVerse { get { return !string.IsNullOrEmpty (this.Verse); } }
      
      /// <summary>
      /// Gets if this link is the about link.
      /// </summary>
      public bool IsAbout { get { return this.FullLink == "About"; } }
      
      /// <summary>
      /// Parses the given link.
      /// </summary>
      /// <param name="link">The link to parse.</param>
      public void Parse (string link)
      {
         this.m_Link = HttpUtility.UrlDecode (link);
         if (string.IsNullOrEmpty (link)) return;
         if (!this.m_Link.Contains (":")) return;
         var linkSplit = this.m_Link.Split (':');
         this.Book = linkSplit [0];
         if (linkSplit.Length == 4)
         {
            this.SubBook = linkSplit [1];
            this.Chapter = linkSplit [2];
            this.Verse = linkSplit [3];
         }
         else if (linkSplit.Length == 3)
         {
            this.SubBook = linkSplit [1];
            this.Chapter = linkSplit [2];
         }
         else
         {
            this.Chapter = linkSplit [1];
         }
      }
   }
}
